package cn.netswap.jeegorilla.framework.listener;


import cn.netswap.jeegorilla.framework.scheduler.quartz.GorillaJobFactory;
import cn.netswap.jeegorilla.framework.scheduler.quartz.GorillaSchedulerFactory;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

@Configuration
public class StartSchedulerListener implements ApplicationListener<ContextRefreshedEvent> {

    Logger log = LoggerFactory.getLogger(StartSchedulerListener.class);

    @Autowired
    public GorillaSchedulerFactory gorillaSchedulerFactory;

    @Autowired
    private GorillaJobFactory GorillaJobFactory;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        try {
            gorillaSchedulerFactory.scheduleJobs();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    //注入SchedulerFactoryBean
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setJobFactory(GorillaJobFactory);
        return schedulerFactoryBean;
    }
}
